import {isEmpty} from "../../js/commonUtils";

export const setFontFamily = (self, textRef, value) => {
  let font = value;
  switch (value) {
    case "serif":
      font = "Lora";
      break;
    case "monospace":
      font = "Poppins";
      break;
    default:
      font = value;
  }
  let selection = self.$refs[textRef].quill.getSelection();
  if(selection.index>-1 && selection.length>0) {
    self.$refs[textRef].quill.formatText(selection.index, selection.length, {fontFamily: font});
  }
}

export const setFontSize = (self, textRef, value) => {
  let fontSize;
  switch (value) {
    case "small":
      fontSize = "10px";
      break;
    case "large":
      fontSize = "18px";
      break;
    case "huge":
      fontSize = "32px";
      break;
    default:
      fontSize = value;
  }
  // console.log("fontSize--------"+fontSize);
  let selection = self.$refs[textRef].quill.getSelection();
  if(selection.index>-1 && selection.length>0) {
    self.$refs[textRef].quill.formatText(selection.index, selection.length, "size", fontSize);
  }
}

export const replaceHtmlTagAttr = (html, tag, attr, val) => {
  if(isEmpty(html) || isEmpty(tag) || isEmpty(attr) || isEmpty(val)) {
    return html;
  }

  let reg = new RegExp('<' + tag + '[^>]*(' + attr + '=[\'\"](\\w*%?)[\'\"])?[^>]*>', 'gi');
  return html.replace(reg, function (match) {
    if(match.indexOf(attr) > 0) {
      let subReg = new RegExp(attr + '=[\'\"](\\w*%?)[\'\"]', 'gi');
      return match.replace(subReg, attr +'="' + val+'"');
    }else{
      return match;
    }
  });
}
